package com.cosmoplat.xikai.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cosmoplat.xikai.business.domain.*;
import com.cosmoplat.xikai.business.domain.dto.scada.DeviceFaultDto;
import com.cosmoplat.xikai.business.domain.mes.result.AlarmDataStatisticsResult;
import com.cosmoplat.xikai.business.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DeviceFaultServiceImpl implements DeviceFaultService {
    @Autowired
    private DeviceLogService deviceLogService;
    @Autowired
    private DeviceThingModelService deviceThingModelService;

    /**
     * 设备故障分页查询
     *
     * @param deviceFaultDto
     * @return
     */
    @Override
    public List<DeviceFault> queryForPage(DeviceFaultDto deviceFaultDto) {
        ArrayList<DeviceFault> deviceLogArrayList = deviceLogService.queryForPage(deviceFaultDto);
        return deviceLogArrayList;
    }

    /**
     * 能耗管理分页查询
     *
     * @param deviceFaultDto
     * @return
     */
    @Override
    public List<EnergyManagement> queryEnergyManagementForPage(DeviceFaultDto deviceFaultDto) {
        return deviceLogService.queryEnergyManagementForPage(deviceFaultDto);


    }

    /**
     * 报警统计
     *
     * @param deviceFaultDto
     * @return
     */
    @Override
    public AlarmDataStatisticsResult alarmDataStatistics(DeviceFaultDto deviceFaultDto) {
        //根据物模型数据表查询报警的数据以 id 为 key 以 identifier 为 value
        List<DeviceThingModel> deviceThingModelList = deviceThingModelService.list(new LambdaQueryWrapper<DeviceThingModel>().eq(DeviceThingModel::getType, 3));
        Map<Long, String> deviceThingModelMap = deviceThingModelList.stream()
                .collect(Collectors.toMap(DeviceThingModel::getId, DeviceThingModel::getIdentifier));
        //获取该时间段段所有报警
        List<DeviceLog> list = deviceLogService.list(new LambdaQueryWrapper<DeviceLog>()
                .eq(DeviceLog::getType, 3)
                .ge(DeviceLog::getDataTime, deviceFaultDto.getStartTime())
                .le(DeviceLog::getDataTime, deviceFaultDto.getEndTime()));
        AlarmDataStatisticsResult alarmDataStatisticsResult = new AlarmDataStatisticsResult();
        //根据报警进行统计获取数量
        list.forEach(deviceLog -> {
            if (!deviceLog.getVal().equals("0")) {
                String value = deviceThingModelMap.get(deviceLog.getDeviceThingModelId());
                switch (value) {
                    case "mechanicalArmRelated":
                        alarmDataStatisticsResult.setMechanicalArmRelated(alarmDataStatisticsResult.getMechanicalArmRelated() + 1);
                        break;
                    case "motor1":
                        alarmDataStatisticsResult.setMotor1(alarmDataStatisticsResult.getMotor1() + 1);
                        break;
                    case "motor2":
                        alarmDataStatisticsResult.setMotor2(alarmDataStatisticsResult.getMotor2() + 1);
                        break;
                    case "motor3":
                        alarmDataStatisticsResult.setMotor3(alarmDataStatisticsResult.getMotor3() + 1);
                        break;
                    case "motor4":
                        alarmDataStatisticsResult.setMotor4(alarmDataStatisticsResult.getMotor4() + 1);
                        break;
                    case "motor5":
                        alarmDataStatisticsResult.setMotor5(alarmDataStatisticsResult.getMotor5() + 1);
                        break;
                    case "motor6":
                        alarmDataStatisticsResult.setMotor6(alarmDataStatisticsResult.getMotor6() + 1);
                        break;
                    case "motor7":
                        alarmDataStatisticsResult.setMotor7(alarmDataStatisticsResult.getMotor7() + 1);
                        break;
                    case "motor8":
                        alarmDataStatisticsResult.setMotor8(alarmDataStatisticsResult.getMotor8() + 1);
                        break;
                    case "motor9":
                        alarmDataStatisticsResult.setMotor9(alarmDataStatisticsResult.getMotor9() + 1);
                        break;
                    case "motor10":
                        alarmDataStatisticsResult.setMotor10(alarmDataStatisticsResult.getMotor10() + 1);
                        break;
                    case "motor11":
                        alarmDataStatisticsResult.setMotor11(alarmDataStatisticsResult.getMotor11() + 1);
                        break;
                    case "motor12":
                        alarmDataStatisticsResult.setMotor12(alarmDataStatisticsResult.getMotor12() + 1);
                        break;
                    case "motor13":
                        alarmDataStatisticsResult.setMotor13(alarmDataStatisticsResult.getMotor13() + 1);
                        break;
                    case "visionSystemRelated":
                        alarmDataStatisticsResult.setVisionSystemRelated(alarmDataStatisticsResult.getVisionSystemRelated() + 1);
                        break;
                    case "otherRelated":
                        alarmDataStatisticsResult.setOtherRelated(alarmDataStatisticsResult.getOtherRelated() + 1);
                        break;
                    case "insufficientOilSupplySystem1":
                        alarmDataStatisticsResult.setInsufficientOilSupplySystem1(alarmDataStatisticsResult.getInsufficientOilSupplySystem1() + 1);
                        break;
                    case "insufficientOilSupplySystem2":
                        alarmDataStatisticsResult.setInsufficientOilSupplySystem2(alarmDataStatisticsResult.getInsufficientOilSupplySystem2() + 1);
                        break;
                    case "insufficientOilSupplySystem3":
                        alarmDataStatisticsResult.setInsufficientOilSupplySystem3(alarmDataStatisticsResult.getInsufficientOilSupplySystem3() + 1);
                        break;
                }
            }

        });
        return alarmDataStatisticsResult;
    }


}
